//
//  GJFMerchantViewController.m
//  Wedding
//
//  Created by 郭静芬 on 2016/12/20.
//  Copyright © 2016年 郭静芬. All rights reserved.
//

#import "GJFMerchantViewController.h"
#import "GJFMerchantModel.h"
#import "GJFMerchantTableViewCell.h"
#import "GJFMerchantDetailViewController.h"

@interface GJFMerchantViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *merchantArr;
@property (nonatomic, assign) NSInteger page_index;
@end

@implementation GJFMerchantViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.page_index = 1;
    [self getMerchantData];
    [self tableView];
}
#pragma mark - 网络请求商户页的数据
- (void)getMerchantData {
    [self.view showBusyHUD];
    NSDictionary *dict = @{@"page_index":@1};
    [GJFNetworkTool POSTRequestWithUrl:APP_MERCHANT params:dict complete:^(NSDictionary *result) {
        [self.tableView.mj_header endRefreshing];
        [self.view hideBusyHUD];
        if ([result[@"code"] intValue] == 1000) {
            NSArray *array = result[@"data"];
            self.merchantArr = [GJFMerchantModel transformWithArr:array];
            [self.tableView reloadData];
        }
    }];
}
#pragma mark - 网络请求商户页更多的数据
- (void)getMerchantMoreData {
    [self.view showBusyHUD];
    NSDictionary *dict = @{@"page_index":@(++_page_index)};
    [GJFNetworkTool POSTRequestWithUrl:APP_MERCHANT params:dict complete:^(NSDictionary *result) {
        [self.view hideBusyHUD];
        [self.tableView.mj_footer endRefreshing];
        if ([result[@"code"] intValue] == 1000) {
            NSArray *array = result[@"data"];
            [self.merchantArr addObjectsFromArray:[GJFMerchantModel transformWithArr:array]];
            [self.tableView reloadData];
        }
        else {
            [self.view showWarning:result[@"msg"]];
            self.page_index--;
        }
    }];
}
#pragma mark - TableView的分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
#pragma mark - TableView每个分区中row数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.merchantArr.count;
}
#pragma mark - TableView中cell的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GJFMerchantTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MerchantCell"];
    cell.merchantModel = self.merchantArr[indexPath.row];
    return cell;
}
#pragma mark - TableView中row的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 180 * YY;
}
#pragma mark - TableView的跳转方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    GJFMerchantDetailViewController *merDetailVC = [[GJFMerchantDetailViewController alloc] init];
    merDetailVC.hidesBottomBarWhenPushed = YES;
    merDetailVC.mid = [self.merchantArr[indexPath.row] myId];
    [self.navigationController pushViewController:merDetailVC animated:YES];
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT ) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[GJFMerchantTableViewCell class] forCellReuseIdentifier:@"MerchantCell"];
        [self.view addSubview:_tableView];
        
        MJRefreshGifHeader *header = [GJFRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(getMerchantData)];
        _tableView.mj_header = header;
        
        _tableView.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMerchantMoreData)];
    }
    return _tableView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
